package com.yuantong.xyerp.core.provider.service;

import com.yuantong.xyerp.core.api.common.Constant;
import com.yuantong.xyerp.core.api.model.Category;
import com.yuantong.xyerp.core.api.service.ICategoryService;
import com.yuantong.xyerp.core.provider.dao.CategoryMapper;
import org.apache.ibatis.annotations.ResultMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jianguo.Yin on 2021-08-04.
 */
@Service
public class CategoryServiceImpl implements ICategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 输入品类等级 返回相应等级的所有品类; level为null时,返回所有品类
     * @param level 品类等级
     * @return
     */
    @Override
    public List<Category> getCategoryByLevel(String level) {
        List<Category> categories = categoryMapper.findByFuzzy();
        List<Category> bigCategory = new ArrayList<>();
        List<Category> levelOneCategory = new ArrayList<>();
        List<Category> levelTwoCategory = new ArrayList<>();
        List<Category> levelThreeCategory = new ArrayList<>();
        for (Category category : categories) {
            switch (category.getCode().length()) {
                case 2:
                    bigCategory.add(category);
                    break;
                case 4:
                    levelOneCategory.add(category);
                    break;
                case 6:
                    levelTwoCategory.add(category);
                    break;
                case 8:
                    levelThreeCategory.add(category);
                    break;
                default:
                    break;
            }
        }
        switch (level) {
            case Constant.BIG_CATEGORY:
                return bigCategory;
            case Constant.LEVEL_ONE:
                return levelOneCategory;
            case Constant.LEVEL_TWO:
                return levelTwoCategory;
            case Constant.LEVEL_THREE:
                return levelThreeCategory;
            default:
                return categories;
        }
    }
}
